﻿using System;
using System.Collections;
using System.Linq;

namespace Deanery.Controller.Directories
{
    class TypeCityController
    {
        private readonly deaneryEntities _db = new deaneryEntities();

        private string[] Divider(string str)
        {
            var split = str.Split('.');
            return split;
        }

        /// <summary>
        /// This method transfers columns name in ukrainian for view table Type_City
        /// </summary>
        /// <returns>Array of string with columns names</returns>
        public Array ColumnName()
        {
            string[] columnName = { "ID", "Тип населеного пункту" };
            return columnName;
        }

        /// <summary>
        /// This method transfers columns data type in table Type_City
        /// </summary>
        /// <returns>Array of string with columns data type</returns>
        public Array ColumnValueType()
        {
            string[] columnDataType = { "guid", "string" };
            return columnDataType;
        }

        /// <summary>
        /// This method select all reccords from table Type_City
        /// </summary>
        /// <returns>Set of IQueryable</returns>
        public IQueryable GetAll()
        {
            var list = _db.Type_City.Select(f => f);
            return list;
        }

        /// <summary>
        /// This method performs INSERT one record to DB in table Type_City
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to insert</param>
        public void Insert(ArrayList reccord)
        {
            var obj = new Type_City
            {
                id_type_city = GenerateGuid(),
                nv_type_city_name = reccord[0].ToString()
            };
            _db.Type_City.AddObject(obj);
            _db.SaveChanges();
        }

        /// <summary>
        /// This method performs UPDATE one record to DB in table Type_City
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="updateGuid">guid value to search reccord in table</param>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to update</param>

        public void Update(Guid updateGuid, ArrayList reccord)
        {
            var obj = (_db.Type_City.Where(t => t.id_type_city == updateGuid)).Single();
            obj.nv_type_city_name = reccord[0].ToString();
            _db.SaveChanges();
        }

        /// <summary>
        /// This method generates new guid
        /// </summary>
        /// <returns>New guid</returns>
        public Guid GenerateGuid()
        {
            var guid = Guid.NewGuid();
            return guid;
        }

        /// <summary>
        /// CORRECT ME!
        /// This method find reccord guid by name in table Type_City
        /// </summary>
        /// <param name="name">string value to search reccord in table</param>
        /// <returns>guid value of finded reccord</returns>
        public Guid FindGuidByName(string name)
        {
            var spliter = Divider(name.Trim());
            string param = spliter[0];
            var obj = (_db.Type_City.Where(t => t.nv_type_city_name == /*spliter[0]*/ param).Select(t => t.id_type_city)).SingleOrDefault();
            return obj;
        }
    }
}
